package cn.itsource.pethome.system.pet.service.impl;

import cn.itsource.pethome.system.menu.domain.Menu;
import cn.itsource.pethome.system.pet.domain.PetType;
import cn.itsource.pethome.system.pet.mapper.PetTypeMapper;
import cn.itsource.pethome.system.pet.service.IPetTypeService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-02-05
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Resource
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> getTree() {
        List<PetType> all = petTypeMapper.findAll();
        ArrayList<PetType> result = new ArrayList<>();
        for (PetType petType : all) {
            if (petType.getPid() == null){//顶级菜单
                result.add(petType);
            }else {
                for (PetType menu1 : all) {
                    if (Objects.equals(menu1.getId(), petType.getPid()))
                        menu1.getChildren().add(petType);
                }
            }
        }
        return result;
    }
}
